package com.ebuy.shopping.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ebuy.shopping.model.Notice;
import com.ebuy.shopping.service.INoticeService;

@Controller
@RequestMapping("notice")
public class NoticeController
{
    @Autowired
    private INoticeService noticeService;

    /*
     * 查看公告详情
     */
    @RequestMapping(value = "{id}/detail", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable
    int id)
    {
        Notice notice = noticeService.load(id);
        model.addAttribute("notice", notice);
        return "notice/noticeDetails";
    }

    /*
     * 后台主页
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index1()
    {
        return "manage/index";
    }

    /*
     * 到公告列表页面
     */
    @RequestMapping("/notices")
    public String notices(Model model)
    {
        List<Notice> notices = noticeService.getNotices();
        model.addAttribute("notices", notices);
        return "notice/notices";
    }

    /*
     * 查询所有公告信息
     */
    @RequestMapping(value = "/index1", method = RequestMethod.GET)
    public String index(Model model)
    {

        List<Notice> notices = noticeService.getNotices();
        model.addAttribute("notices", notices);
        return null;
    }

    /*
     * 到添加公告页面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add()
    {

        return "notice/notice-add";
    }

    /*
     * 执行添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Model model, Notice notice)
    {
        noticeService.add(notice);
        List<Notice> notices = noticeService.getNotices();
        model.addAttribute("notices", notices);
        return "notice/notices";
    }

    /*
     * 到修改页面
     */
    @RequestMapping(value = "{id}/update", method = RequestMethod.GET)
    public String update(@PathVariable
    int id, Model model)
    {

        Notice notice = noticeService.load(id);
        model.addAttribute("notice", notice);

        return "notice/notice-update";
    }

    /*
     * 执行修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Model model, Notice notice)
    {

        noticeService.update(notice);

        List<Notice> notices = noticeService.getNotices();
        model.addAttribute("notices", notices);
        return "notice/notices";
    }

    /*
     * 执行删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(Model model, int id, Notice notice)
    {
        noticeService.delete(id);
        List<Notice> notices = noticeService.getNotices();
        model.addAttribute("notices", notices);
        return "notice/notices";
    }

}
